package com.relax.netty.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.time.LocalDateTime;
import java.util.Set;
import java.util.concurrent.ExecutorService;

import static java.util.concurrent.Executors.defaultThreadFactory;
import static java.util.concurrent.Executors.newSingleThreadExecutor;

public class NioTest12_client {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        final Selector selector = Selector.open();
        socketChannel.register(selector, SelectionKey.OP_CONNECT);
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 6666));
        System.out.println("客户端发送连接请求"+ LocalDateTime.now());

        while (true) {
            selector.select();
            final Set<SelectionKey> selectionKeys = selector.selectedKeys();
            System.out.println("客户端监听到事件"+ LocalDateTime.now());
            for (SelectionKey selectionKey : selectionKeys) {
                if (selectionKey.isConnectable()) {
                    //连接
                    final SocketChannel client = (SocketChannel) selectionKey.channel();
                    if (client.isConnectionPending()) {
                        client.finishConnect();
                        final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                        byteBuffer.put(("client connect:" + LocalDateTime.now()).getBytes());
                        byteBuffer.flip();
                        client.write(byteBuffer);
                        ExecutorService executorService = newSingleThreadExecutor(defaultThreadFactory());
                        executorService.submit(() -> {
                            while (true) {
                                final InputStreamReader inputStreamReader = new InputStreamReader(System.in);
                                final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                                try {
                                    final String readLine = bufferedReader.readLine();
                                    byteBuffer.clear();
                                    byteBuffer.put(readLine.getBytes());
                                    byteBuffer.flip();
                                    client.write(byteBuffer);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                            }

                        });
                    }

                    client.register(selector, SelectionKey.OP_READ);

                } else if (selectionKey.isReadable()) {
                    final SocketChannel client = (SocketChannel) selectionKey.channel();
                    final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                    final int cout = client.read(byteBuffer);
                    if (cout > 0) {
                        final String receivemsg = new String(byteBuffer.array(), 0, cout);
                        System.out.println("收到的数据:" + receivemsg);
                    }
                }
            }
            selectionKeys.clear();
        }
    }
}
